package com.example.log.alarm.constant;


import com.example.log.alarm.queue.ResizableCapacityLinkedBlockingQueue;
import lombok.Getter;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @program: spring-agent
 * @description:
 * @author: zt22328
 * @create: 2023-05-11 14:09
 **/
@Getter
public enum QueueType {

    LINKED_BLOCKING_QUEUE(LinkedBlockingQueue.class) {
        @Override
        public BlockingQueue<Runnable> newBlockingQueue(Integer size) {
            return new LinkedBlockingQueue(size);
        }
    },

    ARRAY_BLOCKING_QUEUE(ArrayBlockingQueue.class) {
        @Override
        public BlockingQueue<Runnable> newBlockingQueue(Integer size) {
            return new ArrayBlockingQueue(size);
        }
    },

    RESIZABLE_CAPACITY_LINKED_BLOCKING_QUEUE(ResizableCapacityLinkedBlockingQueue.class) {
        @Override
        public BlockingQueue<Runnable> newBlockingQueue(Integer size) {
            return new ResizableCapacityLinkedBlockingQueue(size);
        }
    }
    ;

    private Class<? extends BlockingQueue> queueClass;

    QueueType(Class<? extends BlockingQueue> queueClass) {
        this.queueClass = queueClass;
    }

    public static QueueType getQueueType(String clazzName) {
        for (QueueType value : values()) {
            if (value.queueClass.getName().equals(clazzName)) {
                return value;
            }
        }
        return null;
    }

    public static BlockingQueue<Runnable> newBlockingQueue(String clazzName, Integer size) {
        QueueType queueType = getQueueType(clazzName);
        if (queueType == null) {
            return null;
        }
        return queueType.newBlockingQueue(size);
    }

    public abstract BlockingQueue<Runnable> newBlockingQueue(Integer size);


}
